package cc.ipost.ipost;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;
import cc.ipost.ipost.data.RequestData;

@SuppressLint("HandlerLeak")
public class AddressActivity extends Activity {

	ProgressDialog loadingDialog;
	Bundle info;
	EditText nameT;
	EditText zipcodeT;
	EditText addressT;
	EditText phoneT;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.address);

		Intent i = getIntent();
		info = i.getExtras();

		loadingDialog = new ProgressDialog(this, R.style.MyProgressDialog);
		loadingDialog.setMessage(getString(R.string.wait_msg));

		nameT = (EditText) findViewById(R.id.address_name_text);
		zipcodeT = (EditText) findViewById(R.id.address_zipcode_text);
		addressT = (EditText) findViewById(R.id.address_address_text);
		phoneT = (EditText) findViewById(R.id.address_phone_text);
		
		if (info != null){
			nameT.setText(info.getString("name"));
			zipcodeT.setText(info.getString("zipcode"));
			addressT.setText(info.getString("location"));
			phoneT.setText(info.getString("phone"));
		}
		View backBtn = findViewById(R.id.navhome_btn);
		backBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent();
				AddressActivity.this.setResult(RESULT_CANCELED, i);
				AddressActivity.this.finish();
			}
		});

		View saveBtn = findViewById(R.id.address_btn_save);
		View cancelBtn = findViewById(R.id.address_btn_cancel);
		saveBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				submitContact();
			}
		});

		cancelBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent();
				AddressActivity.this.setResult(RESULT_CANCELED, i);
				AddressActivity.this.finish();
			}
		});
	}

	private void submitContact() {
		String name = nameT.getText().toString();
		String zipcode = zipcodeT.getText().toString();
		String address = addressT.getText().toString();
		String phone = phoneT.getText().toString();
		if (name.length() > 0 && zipcode.length() > 0 && address.length() > 0) {
			loadingDialog.show();
			MyApplication ma = (MyApplication) getApplication();
			int type = RequestData.POST;
			String url = String.format(MySettings.UCONTACT, ma.user.ID);
			if (info != null) {
				type = RequestData.PUT;
				url = String.format(MySettings.CONTACT, info.getString("id"));
			}
			RequestData rd = new RequestData(url, type);
			rd.addParam("fname", name);
			rd.addParam("lname", "");
			rd.addParam("zipcode", zipcode);
			rd.addParam("location", address);
			if (phone.length() > 0) {
				rd.addParam("phone", phone);
			}
			WebServiceTask wst = new WebServiceTask(rd, handler);
			wst.execute("");
		} else {
			Toast.makeText(AddressActivity.this, R.string.form_field_warning,
					Toast.LENGTH_SHORT).show();
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			loadingDialog.cancel();
			switch (msg.what) {
			case MySettings.FAIL: {
				Log.e("post or update contact fail", msg.toString());
				new AlertDialog.Builder(AddressActivity.this)
						.setMessage(msg.obj.toString())
						.setNegativeButton("OK", null).setCancelable(true)
						.show();
				break;
			}
			case MySettings.ERR: {
				Log.e("post or update contact err", msg.toString());
				new AlertDialog.Builder(AddressActivity.this)
						.setMessage(msg.obj.toString())
						.setNegativeButton("OK", null).setCancelable(true)
						.show();
				break;
			}
			case MySettings.SUCCESS: {
				Log.e("post or update contact success", msg.toString());
				try {
					JSONObject contact = ((JSONObject) msg.obj)
							.getJSONObject("contact");
					Intent i = new Intent();
					Bundle b = new Bundle();
					b.putString("id", contact.get("id").toString());
					b.putString("name", contact.get("fname").toString());
					b.putString("location", contact.get("location").toString());
					b.putString("zipcode", contact.get("zipcode").toString());
					i.putExtras(b);
					AddressActivity.this.setResult(RESULT_OK, i);
					AddressActivity.this.finish();
				} catch (JSONException e) {
					e.printStackTrace();
				}
				System.out.println(msg.obj);
			}
			}

		}
	};

}
